package org.raymond.icloud.modules.admin.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.raymond.icloud.apis.admin.entity.TSysUser;
import org.raymond.icloud.apis.admin.entity.enums.StateEnum;
import org.raymond.icloud.common.core.constants.MQConstants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class UserConsumer {

    @RabbitHandler
    @RabbitListener(queues = MQConstants.QUEUEE_USER_COMMON)
    public void onMessage(TSysUser tSysUser, Message message, Channel channel) throws IOException {
        log.debug("user:{}, message:{}, channel:{}", tSysUser, message, channel);
        MessageProperties messageProperties = message.getMessageProperties();
        long deliveryTag = messageProperties.getDeliveryTag();
        StateEnum state = tSysUser.getState();
        if(state==StateEnum.NORMAL){
            channel.basicAck(deliveryTag, false);
            log.debug("deliveryTag: {} ack.", deliveryTag);
        }else{
            channel.basicReject(deliveryTag, false);
            log.debug("deliveryTag: {} reject.", deliveryTag);
        }
    }
}
